<?php

class login extends CI_Controller
{
    //登录
    function index()
    {
        if ($this->session->userdata('sin')) {
            redirect('/adminx/');
        }
        $da = $this->pub->get_setting('登录', 'index');
        $this->load->view('adminx/login', $da);
    }

    function dologin()
    {
        $da['type'] = 2;
        if (!$this->session->userdata('sin')) {
            $q = $this->db->get_where('staff', array('phone' => $this->input->post('phone')));
            if ($q->num_rows() > 0) {
                $m = $q->row_array(0);
                if ($m['staff_del'] == 2) {
                    $da['msg'] = '抱歉，您的信息已经被删除！';
                } elseif ($m['type'] == 3) {
                    $da['msg'] = '抱歉，您的账号已经被禁用！';
                } elseif ($m['password'] == md5($this->input->post('password'))) {

                    $q = $this->db->get_where('member', array('mid' => $m['mid']));

                    if ($q->num_rows() > 0) {
                        $mm = $q->row_array(0);
                        $s['head'] = $mm['headimgurl'];
                        $s['min'] = TRUE;
                        $s['mid'] = $mm['mid'];
                        $s['tj_mid'] = $mm['tj_mid'];

                        $s['headx'] = $mm['headimgurl'];
                        $s['midx'] = $mm['mid'];
                    }


                    $s['sin'] = TRUE;
                    $s['sid'] = $m['staff_id'];
                    $s['name'] = $m['name'];
                    $s['state'] = $m['state'];


                    $s['lastmktime'] = mktime();

                    $this->session->set_userdata($s);
                    $da['type'] = 1;
                    $da['msg'] = '登录成功！';
                } else {
                    $da['msg'] = '密码错误！';
                }
            } else {
                $da['msg'] = '账号不存在！';
            }
        }
        echo json_encode($da);
    }

    function erw_login()
    {
        if ($this->session->userdata('sin')) {
            redirect('/adminx/');
        }
        $da = $this->pub->get_setting('二维码登录', 'index');
        if ($this->session->userdata('login_ewm_id')) {
            $da['ewm'] = $this->pub->get_ewm($this->session->userdata('login_ewm_id'), 7);
        } else {
            //获取二维码
            $in['type'] = 1;
            $in['mktime'] = mktime();
            $this->db->insert('ewm_login', $in);
            $id = $this->db->insert_id();
            $this->session->set_userdata('login_ewm_id', $id);
            $da['ewm'] = $this->pub->get_ewm($id, 7);
        }
        $this->load->view('adminx/erw_login', $da);
    }

    //二维码登陆
    function do_erw_login()
    {
        $da['type'] = 2;
        if (!$this->session->userdata('sin')) {
            if ($this->session->userdata('login_ewm_id')) {
                $q = $this->db->get_where('ewm_login', array('id' => $this->session->userdata('login_ewm_id')));
                if ($q->num_rows() > 0) {
                    $ls = $q->row_array(0);
                    if ($ls['type'] == 2) {
                        $q = $this->db->get_where('staff', array('staff_id' => $ls['staff_id']));
                        $m = $q->row_array(0);

                        if ($m['staff_del'] == 1 && $m['type'] < 3) {
                            $q = $this->db->get_where('member', array('mid' => $m['mid']));
                            $mm = $q->row_array(0);

                            $s['sin'] = TRUE;
                            $s['sid'] = $m['staff_id'];
                            $s['name'] = $m['name'];
                            $s['state'] = $m['state'];

                            $s['head'] = $mm['headimgurl'];
                            $s['min'] = TRUE;
                            $s['mid'] = $mm['mid'];
                            $s['tj_mid'] = $mm['tj_mid'];

                            $s['headx'] = $mm['headimgurl'];
                            $s['midx'] = $mm['mid'];

                            $s['lastmktime'] = mktime();

                            $this->session->set_userdata($s);
                            $da['type'] = 1;

                            $this->db->update('ewm_login', array('type' => 1), array('id' => $this->session->userdata('login_ewm_id')));
                        } else {
                            $da['type'] = 3;
                        }

                    }
                    if ($ls['type'] == 3) {
                        $da['type'] = 3;
                    }
                }
            } else {
                $da['cs'] = 2;
            }
        } else {
            $da['cs'] = 1;
        }
        echo json_encode($da);
    }

    //锁屏
    function suop()
    {
        $this->session->set_userdata('sin', FALSE);
        if ($this->session->userdata('sid')) {
            $this->load->view('adminx/suop');
        } else {
            redirect('/iadmin/login');
        }
    }

    function do_suop()
    {
        $da['type'] = 2;
        $q = $this->db->get_where('staff', array('staff_id' => $this->session->userdata('sid')));
        $m = $q->row_array(0);
        if ($m['password'] == md5($this->input->post('password'))) {
            $s['lastmktime'] = mktime();
            $s['sin'] = TRUE;
            $this->session->set_userdata($s);
            $da['type'] = 1;
        }
        echo json_encode($da);
    }

}

?>